package mobi.zeedev.flyingsaucer.common.WebLinkModule.impl;

import android.content.ContentValues;
import com.google.inject.Inject;
import mobi.zeedev.flyingsaucer.common.DataModule.MetaBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.PMap;
import mobi.zeedev.flyingsaucer.common.RegexModule.RegexUtil;
import mobi.zeedev.flyingsaucer.common.RegexModule.impl.ParseException;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.IndividualBeerQuery;
import mobi.zeedev.flyingsaucer.common.WebQueryModule.impl.HttpException;
import mobi.zeedev.flyingsaucer.common.WebQueryModule.impl.Result;
import mobi.zeedev.flyingsaucer.common.WebQueryModule.interfaces.IBeerWebQuery;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

public class IndividualBeerQueryImpl implements IndividualBeerQuery {

    public static final String url = "http://www.beerknurd.com/store.beers.process.php?brew=%d";

    public static final String regex = "\\n\\s+?\\((.*)\\n\\s+?\\)";
    public static final Pattern regex_pattern = Pattern.compile(regex, Pattern.DOTALL);


    public static final String multiRegex = "\\[(.*?)\\] => (.*)";
    public static final Pattern multi_regex_pattern = Pattern.compile(regex, Pattern.MULTILINE);

    @Inject
    IBeerWebQuery beerWebQuery;

    public static final PMap columnMapping = new PMap();

    //  [id] => 4298
    //  [brewer] => 430
    //  [style] => 63
    //  [country] => 1
    //  [description] => Fruity and spicy aromas contribute to the complexity of this golden-colored Belgian Style Ale. The body is medium-full and is very effervescent which contributes to a light, dry impression, despite its strength. The finish is sweet and smooth. 9% abv.
    //  [container] => draught
    //  [name] => Schlafly Grand Cru
    //  [city] => St. Louis, MO
    //  [style_name] => Belgian Style Ale
    //  [country_name] => United States
    //  [brewer_name] => Schlafly Brewing Co
    //  [brewer_city] => St Louis, MO
    //
    //	cb.put(MetaBeer.BEER_ID, mr.group(2) != null ? Integer.valueOf(mr.group(2)) : null);
    //	cb.put(MetaBeer.BREWER_ID, mr.group(4) != null ? Integer.valueOf(mr.group(4)) : null);
    //	cb.put(MetaBeer.STYLE_ID, mr.group(6) != null ? Integer.valueOf(mr.group(6)) : null);
    //	cb.put(MetaBeer.COUNTRY_ID, mr.group(8) != null ? Integer.valueOf(mr.group(8)) : null);
    //
    //	cb.put(MetaBeer.CITY, mr.group(16));
    //	cb.put(MetaBeer.STYLE_NAME, mr.group(18));
    //	cb.put(MetaBeer.COUNTRY_NAME, mr.group(20));
    //	cb.put(MetaBeer.BREWER_NAME, mr.group(22));

//	public IndividualBeerQueryImpl() {
//		this();
//		//this.rawData = rawData;
//	}

    @Inject
    public IndividualBeerQueryImpl() {
        super();
        columnMapping.put("id", MetaBeer.BEER_ID);
        columnMapping.put("brewer", MetaBeer.BREWER_ID);
        columnMapping.put("style", MetaBeer.STYLE_ID);
        columnMapping.put("country", MetaBeer.COUNTRY_ID);
        columnMapping.put("description", MetaBeer.DESCRIPTION);
        columnMapping.put("container", MetaBeer.CONTAINER);
        columnMapping.put("name", MetaBeer.RAW_NAME);
        columnMapping.put("city", MetaBeer.CITY);
        columnMapping.put("country_name", MetaBeer.COUNTRY_NAME);
        columnMapping.put("brewer_name", MetaBeer.BREWER_NAME);
        columnMapping.put("style_name", MetaBeer.STYLE_NAME);
        //style_name
        //columnMapping.put("brewer_city",MetaBeer.);

    }

    private String getUrlForBeerId(Integer beerIdIn) {
        return String.format(url, beerIdIn);
    }

    private ContentValues getContentValue(String rawData) throws ParseException {
        ContentValues cv = new ContentValues();

        String innerMatch = RegexUtil.regexSingleMatch(regex, Pattern.DOTALL, rawData);
//		Log.d(TAG,"Inner match:" + innerMatch);
        List<List<String>> l = RegexUtil.regexMatch(multiRegex, Pattern.MULTILINE, innerMatch);
//		Log.d(TAG,"List-List match:" + l.toString());

        for (List<String> curSubList : l) {
            String key = curSubList.get(0);
            String value = curSubList.get(1);
            String strippedValue = RegexUtil.stripSlashes(value);
            String newKey = columnMapping.get(key);
            if (newKey != null && strippedValue != null) {
//				Log.d(TAG,"Individual Property:" + newKey + "," +  strippedValue);
                cv.put(newKey, strippedValue);
            }
        }

        return cv;
    }

    @Override
    public ContentValues getContentValue(Integer beerId) throws ParseException, HttpException, IOException {
        String data = loadRawData(beerId);
        return getContentValue(data);
    }

    private String loadRawData(Integer beerId) throws HttpException, IOException {
        //getUrlResultHttpGetResusable(getUrlForBeerId(beerId));
        Result r = beerWebQuery.getUrlResultHttpGet(getUrlForBeerId(beerId));
//        this.rawData = r.getContent();
        return r.getContent();
    }


}
